/**
 * 根据
 * @param meter
 * @constructor
 */
import {ElevationColorList} from "./ElevationColorList";
import {ColorManager} from "./ColorManager";

let GetElevationColorWithMeter = (meter: number, elevationColorList?:{[key: number]: string}) => {
    if (elevationColorList === undefined) elevationColorList = ElevationColorList;
    let elevations = Object.keys(elevationColorList).map(e => Number(e));
    elevations.sort((a, b) => a - b);
    if (meter <= elevations[0]) return elevationColorList[elevations[0]];
    if (meter >= elevations[elevations.length - 1])
        return elevationColorList[elevations[elevations.length - 1]];
    //其他情形
    let count = 0;
    while (!(elevations[count] <= meter && meter < elevations[count + 1])) {
        count++;
        if (count >= 40) break;
    }

    let fromElevation = elevations[count], toElevation = elevations[count + 1];
    if (fromElevation !== undefined && toElevation !== undefined) {
        let ratio = (meter - fromElevation) / (toElevation - fromElevation);
        let fromColor = elevationColorList[fromElevation],
            toColor = elevationColorList[toElevation];

        let fc = ColorManager.HEXToRGB(fromColor),
            tc = ColorManager.HEXToRGB(toColor);

        let color0 = ColorManager.getRGBGradientColor([fc, tc], ratio);
        return ColorManager.RGBToHEX(color0.R, color0.G, color0.B);
    }

    return "#FF0";
}

export {GetElevationColorWithMeter};